using System;
using NUnit.Framework;
using dnA.Math;

namespace dnA.dnAtests.Math 
{

    [TestFixture]
    public class ComplexMathTest
    {
        [Test]
        public void ConjugateDouble()
        {
            ComplexDouble cd1 = new ComplexDouble(1.1, -2.2);
            ComplexDouble cd2 = new ComplexDouble(0, -2.2);
            ComplexDouble cd3 = new ComplexDouble(1.1, 0);
            ComplexDouble cd4 = new ComplexDouble(-1.1, 2.2);
            Assert.AreEqual(ComplexMath.Conjugate(cd1), new ComplexDouble(1.1, 2.2));
            Assert.AreEqual(ComplexMath.Conjugate(cd2), new ComplexDouble(0, 2.2));
            Assert.AreEqual(ComplexMath.Conjugate(cd3), new ComplexDouble(1.1, 0));
            Assert.AreEqual(ComplexMath.Conjugate(cd4), new ComplexDouble(-1.1, -2.2));
        }

        [Test]
        public void MaxDouble()
        {
            ComplexDouble cd1 = new ComplexDouble(1.1, -2.2);
            ComplexDouble cd2 = new ComplexDouble(0, -2.2);
            ComplexDouble cdt = ComplexMath.Max(cd1, cd2);
            Assert.AreEqual(cd1, cdt);
        }

        [Test]
        public void NormDouble()
        {
            ComplexDouble cd1 = new ComplexDouble(1.1, -2.2);
            ComplexDouble cd2 = new ComplexDouble(0, -2.2);
            ComplexDouble cd3 = new ComplexDouble(1.1, 0);
            ComplexDouble cd4 = new ComplexDouble(-1.1, 2.2);
            Assert.AreEqual(ComplexMath.Norm(cd1), 2.45967477524977, 1e-15);
            Assert.AreEqual(ComplexMath.Norm(cd2), 2.2, 1e-15);
            Assert.AreEqual(ComplexMath.Norm(cd3), 1.1, 1e-15);
            Assert.AreEqual(ComplexMath.Norm(cd4), 2.45967477524977, 1e-15);
        }

        [Test]
        public void PolarDouble()
        {
            ComplexDouble cd1 = new ComplexDouble(1.1, -2.2);
            ComplexDouble cd2 = new ComplexDouble(0, -2.2);
            ComplexDouble cd3 = new ComplexDouble(1.1, 0);
            ComplexDouble cd4 = new ComplexDouble(-1.1, 2.2);
            ComplexDouble cdt = ComplexMath.Polar(cd1);
            Assert.AreEqual(cdt.Real, 2.45967477524977, 1e-15);
            Assert.AreEqual(cdt.Imag, -1.10714871779409, 1e-15);
            cdt = ComplexMath.Polar(cd2);
            Assert.AreEqual(cdt.Real, 2.2, 1e-15);
            Assert.AreEqual(cdt.Imag, -1.5707963267949, 1e-12);
            cdt = ComplexMath.Polar(cd3);
            Assert.AreEqual(cdt.Real, 1.1, 1e-15);
            Assert.AreEqual(cdt.Imag, 0, 1e-15);
            cdt = ComplexMath.Polar(cd4);
            Assert.AreEqual(cdt.Real, 2.45967477524977, 1e-12);
            Assert.AreEqual(cdt.Imag, 2.0344439357957, 1e-12);
        }

		[Test]
		public void ConjugateFloat()
		{
			ComplexFloat cd1 = new ComplexFloat(1.1f, -2.2f);
			ComplexFloat cd2 = new ComplexFloat(0, -2.2f);
			ComplexFloat cd3 = new ComplexFloat(1.1f, 0);
			ComplexFloat cd4 = new ComplexFloat(-1.1f, 2.2f);
			Assert.AreEqual(ComplexMath.Conjugate(cd1), new ComplexFloat(1.1f, 2.2f));
			Assert.AreEqual(ComplexMath.Conjugate(cd2), new ComplexFloat(0, 2.2f));
			Assert.AreEqual(ComplexMath.Conjugate(cd3), new ComplexFloat(1.1f, 0));
			Assert.AreEqual(ComplexMath.Conjugate(cd4), new ComplexFloat(-1.1f, -2.2f));
		}

		[Test]
		public void MaxFloat()
		{
			ComplexFloat cd1 = new ComplexFloat(1.1f, -2.2f);
			ComplexFloat cd2 = new ComplexFloat(0, -2.2f);
			ComplexFloat cdt = ComplexMath.Max(cd1, cd2);
			Assert.AreEqual(cd1, cdt);
		}

		[Test]
		public void NormFloat()
		{
			ComplexFloat cd1 = new ComplexFloat(1.1f, -2.2f);
			ComplexFloat cd2 = new ComplexFloat(0, -2.2f);
			ComplexFloat cd3 = new ComplexFloat(1.1f, 0);
			ComplexFloat cd4 = new ComplexFloat(-1.1f, 2.2f);
			Assert.AreEqual(ComplexMath.Norm(cd1), 2.45967477524977, 1e-7);
			Assert.AreEqual(ComplexMath.Norm(cd2), 2.2, 1e-7);
			Assert.AreEqual(ComplexMath.Norm(cd3), 1.1, 1e-7);
			Assert.AreEqual(ComplexMath.Norm(cd4), 2.45967477524977, 1e-7);
		}

		[Test]
		public void PolarFloat()
		{
			ComplexFloat cd1 = new ComplexFloat(1.1f, -2.2f);
			ComplexFloat cd2 = new ComplexFloat(0, -2.2f);
			ComplexFloat cd3 = new ComplexFloat(1.1f, 0);
			ComplexFloat cd4 = new ComplexFloat(-1.1f, 2.2f);
			ComplexFloat cdt = ComplexMath.Polar(cd1);
			Assert.AreEqual(cdt.Real, 2.45967477524977, 1e-7);
			Assert.AreEqual(cdt.Imag, -1.10714871779409, 1e-7);
			cdt = ComplexMath.Polar(cd2);
			Assert.AreEqual(cdt.Real, 2.2, 1e-7);
			Assert.AreEqual(cdt.Imag, -1.5707963267949, 1e-7);
			cdt = ComplexMath.Polar(cd3);
			Assert.AreEqual(cdt.Real, 1.1, 1e-7);
			Assert.AreEqual(cdt.Imag, 0, 1e-7);
			cdt = ComplexMath.Polar(cd4);
			Assert.AreEqual(cdt.Real, 2.45967477524977, 1e-7);
			Assert.AreEqual(cdt.Imag, 2.0344439357957, 1e-7);
		}
    }
}
